package com.shanghaionstar.weight;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.onstar.cn.common.util.BitmapUtil;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.vehicle.entity.DiagnosticElement;
import com.shanghaionstar.R;

import java.util.List;

/**
 * 胎压显示view
 * Created by Yonglang Hong on 2016/8/23.
 */
public class PsiView extends View {

    TextPaint paint = new TextPaint();

    int mark_margin = 60;

    int markHeight = 20;
    int markWidth  = 20;
    int statusMarkWidth = 20;
    int carWidth;
    int carHeight;
    int topPadding = 150,bottomPadding = 150,leftPadding = 120,rightPadding = 120;

    private String defStr = "---";

    List<DiagnosticElement> psiList;

    public PsiView(Context context) {
        super(context);
        init();
    }

    public PsiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PsiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void updatePsiStatus(List<DiagnosticElement> psiList){
        this.psiList = psiList;
        postInvalidate();
    }

    private void init(){
        paint.setTextSize(getResources().getDimensionPixelSize(R.dimen.font_sub_title));
        paint.setColor(ContextCompat.getColor(getContext(),R.color.carmanage_tire_color_27344B));
        paint.setAntiAlias(true);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(),R.drawable.mark_green,options);
        markHeight = options.outWidth;
        markWidth = options.outHeight;
        BitmapFactory.decodeResource(getResources(),R.drawable.status_car,options);
        carWidth = options.outWidth;
        carHeight = options.outHeight;
        BitmapFactory.decodeResource(getResources(),R.drawable.status_green,options);
        statusMarkWidth = options.outWidth;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measure();
    }

    private void measure(){
        int width = (markWidth + mark_margin ) * 2 + carWidth + leftPadding + rightPadding;
        int height = carHeight + topPadding + bottomPadding;
        setMeasuredDimension(width,height);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawPsi(canvas);
    }

    private void drawPsi(Canvas canvas){
        drawCarBg(canvas);
        if (psiList != null && psiList.size() > 0) {
            for (DiagnosticElement dEleBean : psiList) {
                switch (dEleBean.getName().toUpperCase()) {
                    case "TIRE PRESSURE LF":               //左前轮
                        drawLFPsi(canvas, dEleBean);
                        break;
                    case "TIRE PRESSURE LR":               //左后轮
                        drawLRPsi(canvas, dEleBean);
                        break;
                    case "TIRE PRESSURE RF":               //右前轮
                        drawRFPsi(canvas, dEleBean);
                        break;
                    case "TIRE PRESSURE RR":               //右后轮
                        drawRRPsi(canvas, dEleBean);
                        break;
                    case "TIRE PRESSURE PLACARD FRONT":    //前胎压参考值
                        drawFrontRefValue(canvas, dEleBean);
                        break;
                    case "TIRE PRESSURE PLACARD REAR":     //后胎压参考值
                        drawBackRefValue(canvas, dEleBean);
                        break;
                }
            }
        }else {
            drawDefaultText(canvas);
        }
    }

    private void drawDefaultText(Canvas canvas){
        int x = (getMeasuredWidth()-(int)paint.measureText(getResources().getString(R.string.carnamage_tire_font))) /2;
        int y = (int)(paint.descent() - paint.ascent());
        drawPsiText(canvas,getResources().getString(R.string.carnamage_tire_font),x,y);
        x = (getMeasuredWidth()-(int)paint.measureText(getResources().getString(R.string.carmanage_tire_back))) /2;
        y = getMeasuredHeight() - 5;
        drawPsiText(canvas,getResources().getString(R.string.carmanage_tire_back),x,y);
        x = 20;
        y = markHeight + topPadding + mark_margin - (int)(paint.ascent() - paint.descent());
        drawPsiText(canvas,defStr,x,y);
        x = getMeasuredWidth() - (int)paint.measureText(defStr)-20;
        y = markHeight + topPadding + mark_margin - (int)(paint.ascent() - paint.descent());
        drawPsiText(canvas,defStr,x,y);
        x = 20;
        y = getMeasuredHeight() - bottomPadding - markHeight - mark_margin;
        drawPsiText(canvas,defStr,x,y);
        x = getMeasuredWidth() - (int)paint.measureText(defStr)-20;
        y = getMeasuredHeight() - bottomPadding - markHeight - mark_margin;
        drawPsiText(canvas,defStr,x,y);

    }

    private void drawCarBg(Canvas canvas){
        drawPsiImg(canvas,R.drawable.status_car,leftPadding+markWidth+mark_margin,topPadding);
    }

    private void drawFrontRefValue(Canvas canvas, DiagnosticElement dEleBean){
        int x = (getMeasuredWidth()-(int)paint.measureText(getResources().getString(R.string.carnamage_tire_font))) /2;
        int y = 0;
        if (CommonUtil.isNull(dEleBean.getUnit()))
            dEleBean.setUnit("");
        StaticLayout layout = new StaticLayout(
                getResources().getString(R.string.carnamage_tire_font) + "\n" + getIntValue(dEleBean.getValue()) + dEleBean.getUnit(),
                paint, (int)paint.measureText(getResources().getString(R.string.carnamage_tire_font)),Layout.Alignment.ALIGN_CENTER, 1.0F, 5.0F, true);
        canvas.save();
        canvas.translate(x, y);//从x，y开始画
        layout.draw(canvas);
        canvas.restore();//别忘了restore
//        drawPsiText(canvas,getResources().getString(R.string.carnamage_tire_font) + "\n" + dEleBean.getValue() + dEleBean.getUnit(),x,y);
    }

    private void drawBackRefValue(Canvas canvas, DiagnosticElement dEleBean){
        int x = (getMeasuredWidth()-(int)paint.measureText(getResources().getString(R.string.carmanage_tire_back))) /2;
        int y = getMeasuredHeight() - (int)(paint.descent()-paint.ascent())*2 - 5;
        if (CommonUtil.isNull(dEleBean.getUnit()))
            dEleBean.setUnit("");
        StaticLayout layout = new StaticLayout(
                getResources().getString(R.string.carmanage_tire_back) + "\n" + getIntValue(dEleBean.getValue()) + dEleBean.getUnit(),
                paint, (int)paint.measureText(getResources().getString(R.string.carmanage_tire_back)),Layout.Alignment.ALIGN_CENTER, 1.0F, 5.0F, true);
        canvas.save();
        canvas.translate(x, y);//从x，y开始画
        layout.draw(canvas);
        canvas.restore();//别忘了restore
//        drawPsiText(canvas,getResources().getString(R.string.carmanage_tire_back) + "\n" + dEleBean.getValue() + dEleBean.getUnit(),x,y);
    }

    private void drawLFPsi(Canvas canvas,DiagnosticElement dEleBean){
        int x = 0;
        int y = markHeight + topPadding + mark_margin - (int)(paint.ascent() - paint.descent());
        int cX = leftPadding;
        int cY = topPadding + mark_margin;
        drawPsiStaus(canvas,dEleBean,x,y,cX,cY);
    }

    private void drawRFPsi(Canvas canvas,DiagnosticElement dEleBean){
        int x = getMeasuredWidth() - (int)paint.measureText(dEleBean.getValue()+dEleBean.getUnit());
        int y = markHeight + topPadding + mark_margin - (int)(paint.ascent() - paint.descent());
        int cX = getMeasuredWidth() - markWidth - rightPadding;
        int cY = topPadding + mark_margin;
        drawPsiStaus(canvas,dEleBean,x,y,cX,cY);
    }

    private void drawLRPsi(Canvas canvas,DiagnosticElement dEleBean){
        int x = 0;
        int y = getMeasuredHeight() - bottomPadding - markHeight - mark_margin;
        int cX = leftPadding;
        int cY = getMeasuredHeight() - bottomPadding - markHeight - mark_margin/2;
        drawPsiStaus(canvas,dEleBean,x,y,cX,cY);
    }

    private void drawRRPsi(Canvas canvas,DiagnosticElement dEleBean){
        int x = getMeasuredWidth() - (int)paint.measureText(dEleBean.getValue()+dEleBean.getUnit());
        int y = getMeasuredHeight() - bottomPadding - markHeight - mark_margin;
        int cX = getMeasuredWidth() - markWidth - rightPadding;
        int cY = getMeasuredHeight() - bottomPadding - markHeight - mark_margin/2;
        drawPsiStaus(canvas,dEleBean,x,y,cX,cY);
    }

    private void drawPsiImg(Canvas canvas , int resId,int x,int y){
        canvas.drawBitmap(BitmapUtil.readBitMap(getContext(), resId),x,y,paint);
    }

    private void drawPsiText(Canvas canvas , String text,int x,int y){
        canvas.drawText(text,x,y,paint);
    }

    private void drawPsiStaus(Canvas canvas,DiagnosticElement dEleBean,int tX,int tY,int icX,int icY){
        if (CommonUtil.isNull(dEleBean.getUnit()))
            dEleBean.setUnit("");
        drawPsiText(canvas, getIntValue(dEleBean.getValue()) + dEleBean.getUnit(),tX,tY);
        int sX = icX < getMeasuredWidth()/2 ? icX + mark_margin + markWidth : icX - mark_margin - statusMarkWidth;
        int sY = icY < getMeasuredHeight()/2 ? icY + mark_margin : icY - mark_margin;
        switch (dEleBean.getMessage().toUpperCase()) {
            case "RED":
                drawPsiImg(canvas,R.drawable.mark_red,icX,icY);
                drawPsiImg(canvas,R.drawable.status_red,sX ,sY);
                break;
            case "GREEN":
                drawPsiImg(canvas,R.drawable.mark_green,icX,icY);
                drawPsiImg(canvas,R.drawable.status_green,sX,sY);
                break;
            case "YELLOW":
                drawPsiImg(canvas,R.drawable.mark_yellow,icX,icY);
                drawPsiImg(canvas,R.drawable.status_yellow,sX,sY);
                break;
        }
    }

    //取整(若是小于0则显示--)
    private String getIntValue(String inputValue){
        if (CommonUtil.isAllNumerice(inputValue)) {
            float value = Float.parseFloat(inputValue);
            if (value < 0)
                inputValue = defStr;
            else
                inputValue = String.valueOf((int)value);
        }else {
            inputValue = defStr;
        }
        return inputValue;
    }

}
